package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ReceivedPaymentDetailInfo;
import com.sl.au.entity.User;
import com.sl.au.repository.ReceivedPaymentDetailInfoRepository;
import com.sl.au.service.ReceivedPaymentDetailInfoService;
import com.sl.au.service.OperateLogService;

@Service("ReceivedPaymentDetailInfoService")
@Transactional
public class ReceivedPaymentDetailInfoServiceImp extends BaseServiceImpl implements ReceivedPaymentDetailInfoService{
	@Autowired
	private ReceivedPaymentDetailInfoRepository receivedPaymentDetailInfoRepository;
	@Autowired
	private OperateLogService operateLogService;
	
	@Override
	public ReceivedPaymentDetailInfoRepository getRepository()
	{
	   return	this.receivedPaymentDetailInfoRepository;
	}
	
	@Override
	public List<ReceivedPaymentDetailInfo> getPage(int start, int length, String key)
	{
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.ASC, "dReceivedPaymentPlanAccDate");
		Page<ReceivedPaymentDetailInfo> pages = receivedPaymentDetailInfoRepository.getPage(key, pageable);
		return pages.getContent();		
	}
	
	@Override
	public long getCount(String key)
	{
		long count =receivedPaymentDetailInfoRepository.getCount(key);
		return count;
	}
	
	
	@Override
	public int save(ReceivedPaymentDetailInfo receivedPaymentDetailInfo, User user) {
		int code=0;
		if(code==0){
			this.receivedPaymentDetailInfoRepository.save(receivedPaymentDetailInfo);
			operateLogService.log(user, "新增详细回款", "新增了详细回款"+receivedPaymentDetailInfo.getnReceivedPaymentDetailPrice());
		}
		return code;
	}
	
	@Override
	public int delete(String id, User user) throws Exception {
		int code=1;
		ReceivedPaymentDetailInfo old = receivedPaymentDetailInfoRepository.findOne(id);
		
		if(null!=old){
			receivedPaymentDetailInfoRepository.delete(old);
			code=0;
			operateLogService.log(user, "删除详细回款", "删除了详细回款"+old.getnReceivedPaymentDetailPrice());
		}		
		return code;
	}
	
	@Override
	public int patch(ReceivedPaymentDetailInfo receivedPaymentDetailInfo, User user) {
		int code=0;
		if(code==0){
			ReceivedPaymentDetailInfo old = this.receivedPaymentDetailInfoRepository.findOne(receivedPaymentDetailInfo.getId());
			
			if(null!=old){
				BeanUtils.copyProperties(receivedPaymentDetailInfo, old, "createTime");
				this.receivedPaymentDetailInfoRepository.save(old);
				operateLogService.log(user, "修改详细回款", "修改了详细回款"+old.getnReceivedPaymentDetailPrice());
			}			
		}
		
		
		
		return code;
	}
}
